from alipay import AliPay
from django.http import HttpResponseBadRequest, JsonResponse
from django.shortcuts import render

# Create your views here.
from django.views import View

from apps.orders.models import OrderInfo
from apps.payment.models import Payment
from meiduo_mall import settings
from utils.views import LoginRequiredJSONMixin


class paymantView(LoginRequiredJSONMixin, View):


    def get(self, request, order_id):
        user = request.user

        try:
            # 校验订单是否正确
            order = OrderInfo.objects.get(order_id=order_id, user=user,
                                          status=OrderInfo.ORDER_STATUS_ENUM['UNPAID'])
        except OrderInfo.DoesNotExist:
            return HttpResponseBadRequest('订单信息错误')

        app_private_key_string = open(settings.APP_PRIVATE_KEY_PATH).read()
        alipay_public_key_string = open(settings.ALIPAY_PUBLIC_KEY_PATH).read()
        # 创建支付宝支付对象

        alipay = AliPay(
            appid=settings.ALIPAY_APPID,
            app_notify_url=None,  # 默认回调url
            app_private_key_string=app_private_key_string,
            # 支付宝的公钥，验证支付宝回传消息使用，不是你自己的公钥,
            alipay_public_key_string=alipay_public_key_string,
            sign_type="RSA2",
            debug=True
        )

        # 生成登录支付宝连接
        order_string = alipay.client_api(
            "alipay.trade.page.pay",
            biz_content={
                "out_trade_no": order_id,
                "total_amount": str(order.total_amount),
                "subject": "美多{}".format(order.order_id),
                'product_code':'FAST_INSTANT_TRADE_PAY'
            },
            return_url= settings.ALIPAY_RETURN_URL  # this is optional
        )

        # 响应登录支付宝连接
        # 真实环境电脑网站支付网关：https://openapi.alipay.com/gateway.do? + order_string
        # 沙箱环境电脑网站支付网关：https://openapi.alipaydev.com/gateway.do? + order_string
        alipay_url = settings.ALIPAY_URL + "?" + order_string

        print('alipay_url', alipay_url)
        return JsonResponse({'code': 0, 'errmsg': 'OK', 'alipay_url': alipay_url})


class PaymentStatusView(View):

    def put(self,request):
        #  获取前端传入的请求参数
        query_dict = request.GET
        data = query_dict.dict()
        # 获取并从请求参数中提出signature
        signature = data.pop('sign')


        # 创建支付宝支付对象
        app_private_key_string = open(settings.APP_PRIVATE_KEY_PATH).read()
        alipay_public_key_string = open(settings.ALIPAY_PUBLIC_KEY_PATH).read()
        alipay = AliPay(
            appid=settings.ALIPAY_APPID,
            app_notify_url=None,  # 默认回调url
            app_private_key_string=app_private_key_string,
            # 支付宝的公钥，验证支付宝回传消息使用，不是你自己的公钥,
            alipay_public_key_string=alipay_public_key_string,
            sign_type="RSA2",
            debug=True
        )

        # 校验这个重定向是否是alipay重定向过来的
        success = alipay.verify(data,signature)

        if success:
            # 读取order_id
            order_id = data.get('out_trade_no')
            # 读取支付宝流水号
            trade_id = data.get('trade_no')
            # 保存Payment模型类数据
            Payment.objects.create(
               order_id = order_id,
               trade_id=trade_id
            )

            # 修改订单状态为待评价
            OrderInfo.objects.filter(order_id=order_id,status=OrderInfo.ORDER_STATUS_ENUM['UNPAID']).update(
                status=OrderInfo.ORDER_STATUS_ENUM['UNSEND']
            )

            # 响应trade_id
            return JsonResponse({'code':0,'errmsg':'OK','trade_id':trade_id})
        else :
            # 订单支付失败重定向到我的订单
            return JsonResponse({'code':400,'errmsg':'非法申请'})






